package com.biz.primus.ms.thirdlogin.oauth.api.alipay;

import com.biz.primus.ms.thirdlogin.oauth.service.alipay.AlipayOAuthService;
import org.scribe.builder.api.DefaultApi20;
import org.scribe.model.OAuthConfig;
import org.scribe.oauth.OAuthService;
import org.scribe.utils.OAuthEncoder;

/**
 * @author jun.liu
 * @date 2017/9/29
 */
public class AlipayApi20 extends DefaultApi20
{
    /**第三方应用授权*/
    private final String AUTHORIZE_URL = "https://openauth.alipay.com/oauth2/appToAppAuth.htm?app_id=%&redirect_uri=%";
    /**interface支付宝各接口请求地址*/
    private final String INTERFACE_URL = "https://openapi.alipay.com/gateway.do";

    private String publicKey;

    @Override
    public String getAccessTokenEndpoint() {
        return INTERFACE_URL;
    }

    public AlipayApi20(String publicKey)
    {
        this.publicKey = publicKey;
    }

    @Override
    public String getAuthorizationUrl(OAuthConfig config)
    {
        return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
    }

    @Override
    public OAuthService createService(OAuthConfig config) {
        return new AlipayOAuthService(this, new AlipayOAuthConfig(config.getApiKey(), config.getApiSecret(), config.getCallback(), config.getSignatureType(), config.getScope(),null, publicKey));
    }

}
